package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.entity.ScenesInfo;
import com.fin.zw.aiqas.model.request.ScenesRequest;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.ScenesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@RequestMapping("/scenes")
@Api(value = "场景管理", description = "场景管理")
public class ScenesController {

    @Autowired
    private ScenesService scenesService;

    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询场景", notes = "查询场景")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<ScenesInfo>> queryScenesPage(ScenesRequest request, Pageable pageable) {
        Page<ScenesInfo> page = scenesService.queryRuleScenes(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/fetch")
    @ApiOperation(value = "查询场景", notes = "查询场景")
    public ResponseEntity<List<ScenesInfo>> queryScenes(ScenesRequest request) {
        return ResponseEntity.ok().body(scenesService.queryRuleScenes(request));
    }

    @PostMapping("/add")
    @ApiOperation(value = "创建质检点大分类", notes = "创建场景")
    public ResponseEntity<String> createScenes(@RequestBody ScenesRequest request,
                                               @ApiIgnore @CurrentUser SessionModel sessionModel) {
        scenesService.createScenes(request, sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }

    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.PATCH})
    @ApiOperation(value = "场景修改", notes = "场景修改")
    public ResponseEntity<String> editRuleScenes(@RequestBody ScenesRequest request,
                                                 @ApiIgnore @CurrentUser SessionModel sessionModel) {
        scenesService.updateScenes(request, sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "场景信息删除", notes = "场景信息删除")
    public ResponseEntity<String> deleteRuleScenes(@PathVariable Long id) {
        scenesService.deleteScenes(id);
        return ResponseEntity.ok().body("删除成功");
    }
}
